﻿using Donal.Xms.Common;
using Donal.Xms.Web.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Donal.Xms.Web.temp.main
{
    public partial class cart : UI.UserPage
    {
        BLL.cart bll = new BLL.cart();
        private DataSet cartGoods;
        private string discount_remark = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            RptBind();
        }

        #region 数据绑定=================================

        private void RptBind()
        {
            cartGoods = bll.GetCartGoodsProductCategoryList(groupModel.id);

            if (Utils.CheckDataSet(cartGoods))
            {
                foreach (DataRow dr in cartGoods.Tables[0].Rows)
                {
                    dr["sell_price"] = GetSpecPrice(Utils.ObjToInt(dr["goods_id"]),
                        dr["title"].ToString(),
                        Utils.ObjToInt(dr["products_id"]),
                        dr["product_title"].ToString(),
                        Utils.ObjToInt(dr["category_id"]),
                        dr["category_title"].ToString(),
                        Utils.ObjToDecimal(dr["sell_price"], 0),
                        out discount_remark
                        );
                }

                // 获取该列的唯一值           
                var distinctValues = cartGoods.Tables[0].AsEnumerable()
                                .Select(row => Convert.ToString(row["products_id"]))
                                .Distinct()
                                .ToList();

                DataTable _tempDt = new DataTable();
                _tempDt.Columns.Add("id");

                for (int i = 0; i < distinctValues.Count; i++)
                {
                    DataRow dr = _tempDt.NewRow();
                    dr[0] = distinctValues[i];
                    _tempDt.Rows.Add(dr);
                }

                rptCategory.DataSource = _tempDt;
                rptCategory.DataBind();
            }

        }

        #endregion

        protected void rptCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater rep = e.Item.FindControl("rptList") as Repeater;

                HiddenField hidId = (HiddenField)e.Item.FindControl("hidId");

                int id = Convert.ToInt32(hidId.Value);

                DataView dv = cartGoods.Tables[0].DefaultView;
                // 应用筛选器
                dv.RowFilter = "products_id=" + id;
                // 应用排序规则（可选）
                dv.Sort = "add_time DESC";

                rep.DataSource = dv;
                rep.DataBind();
            }

        }

        public string GetFields(int goods_id)
        {
            Model.products_goods goods = new BLL.products_goods().GetModel(goods_id);
            if (goods == null || goods.fields == null || goods.fields.Count == 0)
            {
                return "---";
            }
            else
            {
                string fields = "";
                foreach (Model.products_goods_filed f in goods.fields)
                {
                    fields += f.field_title + "：" + f.field_value + "</br>";
                }
                return fields;
            }
        }
    }
}